package com.czy.basecommon.utils.base;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;

import com.czy.basecommon.BR;
import com.czy.basecommon.utils.base.BaseViewModel;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.TUtil;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.ButterKnife;
import me.jessyan.autosize.AutoSizeCompat;

/**
 *
 */
public abstract class BaseVmDataBindingActivity<V extends ViewDataBinding,T extends BaseViewModel>extends AppCompatActivity {
    protected T viewModel;

    protected V binding;

    private int viewModelId;

    public BaseVmDataBindingActivity(){

    }

    protected void setListener() {

    }
    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView(Bundle savedInstanceState);

    /**
     * 初始化ViewModel的id
     *
     * @return BR的id
     */
    public abstract int initVariableId();


      @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        ActivityTool.getInstance().pushOneActivity(this);

          binding = DataBindingUtil.setContentView(this, initContentView(savedInstanceState));
          viewModelId = initVariableId();
          viewModel = VMProviders(this, (Class<T>) TUtil.getInstance(this, 1));
          binding.setVariable(viewModelId, viewModel);


          initUI();
          initUIS(savedInstanceState);
          dataObserver();
          initData();
          setListener();
    }

    protected void initUIS(Bundle savedInstanceState) {

    }


    protected void dataObserver() {

    }
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }
    protected <T extends ViewModel> T VMProviders(AppCompatActivity fragment, @NonNull Class modelClass) {
        return (T) ViewModelProviders.of(fragment).get(modelClass);

    }
    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        ActivityTool.getInstance().popOneActivity(this);
    }

    private void initTypefaces(){

    }
    /**
     * 跳转页面
     *
     * @param clz 所跳转的目的Activity类
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(this, clz));
    }

    /**
     * 跳转页面
     *
     * @param clz    所跳转的目的Activity类
     * @param bundle 跳转所携带的信息
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    protected abstract void initUI();
    protected abstract void initData();


    @Override
    public Resources getResources() {
        //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
        AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));//如果没有自定义需求用这个方法
        return super.getResources();
    }
}